package com.ruoyi.iot.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.ruoyi.iot.service.IIotDeviceControlService;
import com.ruoyi.iot.websocket.IotWebSocket;

/**
 * 设备控制服务实现类
 */
@Service
public class IotDeviceControlServiceImpl implements IIotDeviceControlService {
    
    /**
     * 获取在线设备列表
     */
    @Override
    public List<Map<String, Object>> getOnlineDevices() {
        Map<String, Object> deviceInfoMap = IotWebSocket.getAllDeviceInfo();
        List<Map<String, Object>> deviceList = new ArrayList<>();
        deviceInfoMap.forEach((deviceId, info) -> {
            if (info instanceof Map) {
                @SuppressWarnings("unchecked")
                Map<String, Object> deviceInfo = (Map<String, Object>) info;
                deviceInfo.put("deviceId", deviceId);
                deviceList.add(deviceInfo);
            }
        });
        return deviceList;
    }

    /**
     * 获取设备信息
     */
    @Override
    public Map<String, Object> getDeviceInfo(String deviceId) {
        Object info = IotWebSocket.getDeviceInfo(deviceId);
        if (info instanceof Map) {
            @SuppressWarnings("unchecked")
            Map<String, Object> deviceInfo = (Map<String, Object>) info;
            deviceInfo.put("deviceId", deviceId);
            return deviceInfo;
        }
        return null;
    }

    /**
     * 运行脚本
     */
    @Override
    public void runScript(String deviceId, String scriptName, String scriptCode) {
        IotWebSocket.runScript(deviceId, scriptName, scriptCode);
    }

    /**
     * 停止脚本
     */
    @Override
    public void stopScript(String deviceId) {
        IotWebSocket.stopScript(deviceId);
    }

    /**
     * 获取设备截图
     */
    @Override
    public void getScreenshot(String deviceId) {
        IotWebSocket.requestScreenshot(deviceId);
    }
} 